package myoa

import myoa.tool.StringTool

class CompanyController {

    def create() {

    }

    def save() {
        Company c = new Company(params)
        c.website = StringTool.addUrlProtocol(c.website)
        c.traceCreate()
        if (c.save()) {
            flash.message = message(code: 'default.save.success')
            redirect(action: 'show', id: c.id)
        } else {
            flash.message = message(code: 'default.save.failed')
            render(view: "create", model: [company: c])
        }
    }

    def show(Long id) {
        [company: Company.read(id)]
    }

    def edit(Long id) {
        [company: Company.read(id)]
    }

    def update(Long id) {
        Company c = Company.get(id)
        c.properties = params
        c.website = StringTool.addUrlProtocol(c.website)
        c.traceUpdate()

        if (c.save()) {
            flash.message = message(code: 'default.save.success')
            redirect(action: 'show', id: id)
        } else {
            flash.message = message(code: 'default.save.failed')
            render(view: "edit", model: [company: c])
        }
    }

    def delete(Long id) {
        Company c = Company.get(id)
        c.delete()
        flash.message = message(code: 'default.delete.success', args: [c.name])
        redirect(action: 'list')
    }

    def list() {
        params.max = StringTool.paginateMax(params.max)
        [companies: Company.findAll(params), total: Company.count()]
    }
}
